
<template>
  <div class="register-container">
    <el-card class="register-card">
      <h2 class="register-title">用户登录</h2>
      <el-form :model="form" class="register-form" @submit.native.prevent="handleRegister">
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios'; // 引入 axios

export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    };
  },
  methods: {
    handleRegister() {
      axios.post('http://127.0.0.1:8000/login', this.form) // 发送请求 this.form指的是上面data/form数据；
       .then((response) => {
          // 根据返回的状态判断登录是否成功
          if (response.data.meta.status === 200) {
            this.$message.success(response.data.meta.message || '登录成功');
            console.log('登录成功', response.data);
          } else {
            this.$message.error('登录失败');
            console.error('登录失败', response.data);
          }
        })
       .catch(error => {
          // 处理请求错误
          this.$message.error('登录失败，请重试');
          console.error('请求失败', error.message);
        });
    }
  }
}
</script>

<style scoped>.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.register-card {
  width: 400px;
}

.register-title {
  text-align: center;
  margin-bottom: 20px;
}

.register-form {
  width: 100%;
}
</style>
